﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmManageCommonItem : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstCommonItemEntity mSelectedCommonItem;
        #endregion

        #region Constructor
        public frmManageCommonItem()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsDelete, typeof(DeleteFlag), typeof(string), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colIsDeleted, typeof(DeleteFlag), typeof(int), true);

            DataTable dt = MstCommonItemBs.GetAllItemType();
            DataRow dr = dt.NewRow();
            dr["ITEM_TYPE"] = "";
            dt.Rows.InsertAt(dr, 0);
            cmbItemType.DataSource = dt;
            cmbItemType.DisplayMember = "ITEM_TYPE";
            cmbItemType.ValueMember = "ITEM_TYPE";
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtItemCode.Text) == false)
            {
                condition += " AND ITEM_CODE LIKE '%" + txtItemCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtItemName.Text) == false)
            {
                condition += " AND ITEM_NAME LIKE '%" + txtItemName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtItemValue.Text) == false)
            {
                condition += " AND ITEM_VALUE LIKE '%" + txtItemValue.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbItemType.Text) == false)
            {
                condition += " AND ITEM_TYPE = '" + cmbItemType.Text + "'";
            }
            if (string.IsNullOrEmpty(cmbIsDelete.Text) == false && cmbIsDelete.SelectedValue != null)
            {
                condition += " AND IS_DELETED = " + cmbIsDelete.SelectedValue.ToString() + "";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ITEM_TYPE, ITEM_CODE";
            DataTable dt = MstCommonItemBs.GetMstCommonItemByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        private void frmManageCommonItem_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedCommonItem = new MstCommonItemEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectedCommonItem == null)
            {
                MessageBox.Show("请选择一个共工项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCommonItem frm = new frmMaintainCommonItem();
            frm.ThisActionType = ActionType.View;
            frm.SelectedCommonItem = mSelectedCommonItem;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedCommonItem == null)
            {
                MessageBox.Show("请选择一个共工项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中共工项目？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedCommonItem.IsDeleted = 1;
                bool successful = MstCommonItemBs.UpdateMstCommonItem(mSelectedCommonItem);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedCommonItem == null)
            {
                MessageBox.Show("请选择一个共工项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCommonItem frm = new frmMaintainCommonItem();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedCommonItem = mSelectedCommonItem;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainCommonItem frm = new frmMaintainCommonItem();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        #endregion

    }
}
